package com.biostime.dingdingbox.modules.db.api;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.biostime.dingdingbox.common.api.FastResponse;
import com.biostime.dingdingbox.common.api.ObjResponse;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.db.entity.PraiseRecord;
import com.biostime.dingdingbox.modules.db.service.MemberService;
import com.biostime.dingdingbox.modules.db.service.PraiseRecordService;

/**
 * 玩法攻略API
 * @author chenweicheng
 * @version 2016-06-28
 */
@RestController
@RequestMapping(value = "${adminPath}/db/api/praise")
public class PraiseApi extends BaseController {
	
	
	@Autowired
	private PraiseRecordService praiseRecordService;
	
	@Autowired
	private MemberService memberService;
	
	
	/**
	 * 点赞
	 */
	@RequestMapping(value = "praise")
	public ObjResponse<String> praise(String type,String objectId,String userId,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		userId = memberService.apiGetUserId(userId);
		String message = "点赞成功";
		if(userId != null && !userId.equals("") && type != null && objectId != null){
			if(praiseRecordService.apiGetPraiseRecord(type, objectId, userId) == null){
				PraiseRecord praiseRecord = new PraiseRecord();
				praiseRecord.setType(type);
				praiseRecord.setObjectId(Integer.parseInt(objectId));
				praiseRecord.setMemberId(Integer.parseInt(userId));
				praiseRecord.setCreateDate(new Date()); 
				praiseRecordService.save(praiseRecord); 
			}else{
				message = "点赞失败,你已点过赞";
			}
		}else{
			message = "点赞失败,参数不全";
		}
		return  FastResponse.create().errMsg(message).obj("点赞操作");		
	}	
	
	/**
	 * 取消点赞
	 */
	@RequestMapping(value = "cancelPraise")
	public ObjResponse<String> cancelPraise(String type,String objectId,String userId,HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		userId = memberService.apiGetUserId(userId);
		String message = "取消点赞成功";
		if(userId != null && !userId.equals("") && type != null && objectId != null){
			if(praiseRecordService.apiGetPraiseRecord(type, objectId, userId) != null){
				praiseRecordService.apiCancelPraise(type, objectId, userId); 
			}else{
				message = "取消点赞失败,你还没点过赞";
			}
		}else{
			message = "取消点赞失败,参数不全";
		}
		return  FastResponse.create().errMsg(message).obj("取消点赞操作");		
	}	
}
